/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package evotingclient.votesending;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import javax.xml.bind.DatatypeConverter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


//przyklad z ksiazki
//TCP/IP Sockets in JavaTM: Practical Guide for Programmers, Second Edition" by Kenneth L. Calvert and Michael J. Donahoo
public class TCPEchoClient {
    
    public static final String serverIp = "localhost";
    public static final int servPort = 3737;
    Socket socket;
    InputStream in;
    OutputStream out;
    LengthFramer lf;
    
    public TCPEchoClient(){
        
    }
    public void writeSigned(OutputStream os, org.w3c.dom.Document  domDocument) throws TransformerConfigurationException, TransformerException {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer trans = tf.newTransformer();
        trans.transform(new DOMSource(domDocument), new StreamResult(os));
    }
    public byte[] sendAndReceiveData(byte[] data) throws UnknownHostException, IOException, ParserConfigurationException, SAXException, TransformerConfigurationException, TransformerException{
        socket = new Socket(serverIp, servPort);
        System.out.println("Connected to server...sending echo string");

        in = socket.getInputStream();
        out = socket.getOutputStream();
        lf = new LengthFramer(in);
        lf.frameMsg(data, out);  // Send the encoded string to the server
        
        byte[] result = lf.nextMsg();
        
        in.close();
        out.close();
                    
       return result;
        
    }
    
   
     static private String bytesToString(byte[] bytes){
        return DatatypeConverter.printBase64Binary(bytes);
    }
    
    static private byte[] stringToBytes(String string){
        return DatatypeConverter.parseBase64Binary(string);
    }
    
    public static void main(String[] args) throws IOException, UnknownHostException, ParserConfigurationException, TransformerException, SAXException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, Exception {

        String server = "localhost";       // Server name or IP address

        String dataString = "Thyger, thyger, burning bright in the forest of the night, what immortal hand or eye dare frame thy fearful symmetry";
        // Convert String to bytes using the default character encoding
        byte[] data = stringToBytes(dataString);
        System.out.println(bytesToString(data));
        PublicKeyHandler pkh = new PublicKeyHandler();
        pkh.loadPublicKey(null);
        byte[] encryptedData = pkh.rsaEncrypt(data);
        System.out.println("Connected to server...sending echo string");
        TCPEchoClient client = new TCPEchoClient();
        byte[] result = client.sendAndReceiveData(encryptedData);

        
        
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        org.w3c.dom.Document doc = docBuilder.parse(new ByteArrayInputStream(result));
        NodeList e = doc.getElementsByTagName("Signature");
        System.out.println(e.getLength()+ e.item(0).getNamespaceURI() + e.item(0).getChildNodes());
        String file = "myvote.xml";
        client.writeSigned(new FileOutputStream(new File(file)), doc);
        
    }


    
}
